package com.dudu.dladminserver.service.impl;

import com.dudu.dladminserver.mapper.UserMapper;
import com.dudu.dladminserver.pojo.PageBean;
import com.dudu.dladminserver.pojo.User;
import com.dudu.dladminserver.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

//    @Override
//    public PageBean page(Integer offset, Integer limit) {
//        Long total = userMapper.count();
//        List<User> rows = userMapper.page(offset, limit);
//        PageBean pageBean = new PageBean(total, rows);
//        return pageBean;
//    }

    @Override
    public PageBean page(Integer offset, Integer limit, String username, String phone, String nickname, Short status) {
        PageHelper.offsetPage(offset, limit);
        List<User> rows = userMapper.page(username, phone, nickname, status);
        Page<User> p = (Page<User>) rows;

        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public void delete(List<Integer> ids) {
        userMapper.delete(ids);
    }

    @Override
    public void add(User user) {
        user.setStatus((short) 1);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userMapper.add(user);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public User getById(Integer id) {
        return userMapper.getById(id);
    }

    @Override
    public User login(User user) {
       return userMapper.getByIUsernameAndPassword(user);
    }

}
